/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.previous_flow.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.previous_flow.entity.PreviousFlow;
import com.thinkgem.jeesite.modules.previous_flow.service.PreviousFlowService;

/**
 * 记录上次流量数据Controller
 * @author xlp
 * @version 2019-05-20
 */
@Controller
@RequestMapping(value = "${adminPath}/previous_flow/previousFlow")
public class PreviousFlowController extends BaseController {

	@Autowired
	private PreviousFlowService previousFlowService;
	
	@ModelAttribute
	public PreviousFlow get(@RequestParam(required=false) String id) {
		PreviousFlow entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = previousFlowService.get(id);
		}
		if (entity == null){
			entity = new PreviousFlow();
		}
		return entity;
	}
	
	@RequiresPermissions("previous_flow:previousFlow:view")
	@RequestMapping(value = {"list", ""})
	public String list(PreviousFlow previousFlow, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PreviousFlow> page = previousFlowService.findPage(new Page<PreviousFlow>(request, response), previousFlow); 
		model.addAttribute("page", page);
		return "modules/previous_flow/previousFlowList";
	}

	@RequiresPermissions("previous_flow:previousFlow:view")
	@RequestMapping(value = "form")
	public String form(PreviousFlow previousFlow, Model model) {
		model.addAttribute("previousFlow", previousFlow);
		return "modules/previous_flow/previousFlowForm";
	}

	@RequiresPermissions("previous_flow:previousFlow:edit")
	@RequestMapping(value = "save")
	public String save(PreviousFlow previousFlow, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, previousFlow)){
			return form(previousFlow, model);
		}
		previousFlowService.save(previousFlow);
		addMessage(redirectAttributes, "保存记录上次流量数据成功");
		return "redirect:"+Global.getAdminPath()+"/previous_flow/previousFlow/?repage";
	}
	
	@RequiresPermissions("previous_flow:previousFlow:edit")
	@RequestMapping(value = "delete")
	public String delete(PreviousFlow previousFlow, RedirectAttributes redirectAttributes) {
		previousFlowService.delete(previousFlow);
		addMessage(redirectAttributes, "删除记录上次流量数据成功");
		return "redirect:"+Global.getAdminPath()+"/previous_flow/previousFlow/?repage";
	}

}